package com.rs.net.encoders;

import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;

import com.rs.game.player.Player;
import com.rs.io.OutputStream;
import com.rs.net.Session;

public final class LoginPacketsEncoder extends Encoder {

public LoginPacketsEncoder(Session connection) {
	super(connection);
}

public final void sendStartUpPacket() {
	OutputStream stream = new OutputStream(1);
	stream.writeByte(0);
	session.write(stream);
}

public final void sendClientPacket(int opcode) {
	OutputStream stream = new OutputStream(1);
	stream.writeByte(opcode);
	ChannelFuture future = session.write(stream);
	if (future != null) {
		future.addListener(ChannelFutureListener.CLOSE);
	} else {
		session.getChannel().close();
	}
}

public void sendLobbyDetails(Player player) {
	int ipHash = 0;
	OutputStream stream = new OutputStream();
	stream.writePacketVarByte(player, 2);
	stream.writeByte(player.getRights());//rights
	stream.writeByte(0);
	stream.writeByte(0);
	stream.write24BitInteger(0);
	stream.writeByte(0);
	stream.writeByte(0);
	stream.writeByte(0);
	stream.writeLong(500); //members subscription end
	stream.write5ByteInteger(12);
	stream.writeByte(0x1); //0x1 - if members, 0x2 - subscription
	stream.writeInt(1); 
	stream.writeByte(1); 
	stream.writeInt(1); //recovery questions set date
	stream.writeShort(1); //recovery questions 1 for set, 0 for not set
	stream.writeShort(0); //Messages add support for forum integration
	stream.writeShort(player.getLastLoggedIn() == 0 ? 0 : (int)(((player.getLastLoggedIn() - 1014786000000L) / 86400000) + 1));//last logged in date
	if (player.getLastIP() != null) {
		String[] ipSplit = player.getLastIP().split("\\.");
		ipHash = Integer.parseInt(ipSplit[0]) << 24 | Integer.parseInt(ipSplit[1]) << 16 | Integer.parseInt(ipSplit[2]) << 8 | Integer.parseInt(ipSplit[3]);
	}
	stream.writeInt(ipHash); //ip part
	stream.writeByte(3); //email status (0 - no email, 1 - pending parental confirmation, 2 - pending confirmation, 3 - registered)
	stream.writeShort(0);
	stream.writeShort(0);
	stream.writeByte(0);
	stream.writeGJString(player.getDisplayName());
	stream.writeByte(0);
	stream.writeInt(1);
	stream.writeByte(1);
	stream.writeShort(1); // default world id (should be generated by country ids like we found in client today)
	stream.writeGJString("127.0.0.1");
	stream.endPacketVarByte();
	player.getSession().write(stream);     
}

public final void sendLoginDetails(Player player) {
	OutputStream stream = new OutputStream();
	stream.writePacketVarByte(null, 2);
	stream.writeByte(player.getRights());
	stream.writeByte(0);
	stream.writeByte(0);
	stream.writeByte(0);
	stream.writeByte(1);
	stream.writeByte(0);
	stream.writeShort(player.getIndex());
	stream.writeByte(1);
	stream.write24BitInteger(0);
	stream.writeByte(1); //is member world
	stream.writeString(player.getDisplayName());
	stream.endPacketVarByte();
	session.write(stream);
}
}